package com.yc.cinema;

import com.yc.cinema.dao.UserDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
// 扫描所有的组件, 还能扫描其他的配置文件
@ComponentScan({"com.yc.cinema","com.yc.bank"})
@EnableTransactionManagement
public class AutoIoConfig {

    @Bean
    TransactionManager tm(DataSource dataSource){
        TransactionManager tm = new JdbcTransactionManager(dataSource);
        return tm;
    }

    @Bean
    UserDao myUserDao(){
        return new UserDao();
    }

    // 定义 Spring 数据
    @Bean
    DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///test");
        dataSource.setUsername("root");
        dataSource.setPassword("a");
        return dataSource;
    }

//    @Resource
//    DataSource dataSource;
    // JDBC模板类 , 需要数据源对象
    @Bean
    JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }

}
